/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.Iterator;
import java.util.List;
import org.hibernate.Session;

/**
 * BaseDAO implements common operations to access data source
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class BaseDAO {
	/**
	 * Delete a list of persistence instances
	 * 
	 * @param persistentInstanceList
	 *            a list of persistence instances
	 */
	@SuppressWarnings("rawtypes")
	public void deleteList(List  persistentInstanceList) {

		if (persistentInstanceList != null) {
			for (Iterator  iter = persistentInstanceList.iterator(); iter
					.hasNext();) {
				Object persistentInstance = iter.next();
				deleteSingle(persistentInstance);
			}

		}

	}

	/**
	 * Delete an instance
	 * 
	 * @param persistentInstance
	 *            instance to be deleted
	 */
	public void deleteSingle(Object persistentInstance) {

		Session session = HibernateSessionFactory.getSession();
		session.delete(persistentInstance);

	}

	/**
	 * Merge an instance
	 * 
	 * @param detachedInstance
	 *            the detached instance
	 * @return the merged instance
	 */
	public Object merge(Object detachedInstance) {
		Session session = HibernateSessionFactory.getSession();

		Object result = session.merge(detachedInstance);

		return result;

	}

	/**
	 * Save an instance
	 * 
	 * @param transientInstance
	 */
	public void save(Object transientInstance) {
		Session session = HibernateSessionFactory.getSession();
		session.save(transientInstance);

	}

}
